
ExtractTrisMode = {
CreateUnit = function ()
	ExtractTrisMode.CurFuncSeq = ExtractTrisMode.FuncSeq.Pass1
end,
FuncSeq = {
	Pass1 = function ()
		if not SelectFaceMode.EditObj or not SelectFaceMode.tIndex or table.getn(SelectFaceMode.tIndex)<=0 then
			TRACE("SelectFaceMode - No faces selected!\n")
			ExtractTrisMode.CurFuncSeq = IdleFunc
			return
		end
		TRACE("ExtractTrisMode - Begin\n")
		local EditObj = SelectFaceMode.EditObj
		local MeshName = EditObj:GetMesh()
		TRACE(string.format("ExtractTrisMode - MeshName (%s)\n",MeshName))
		local Mesh = g_MgrMesh:Find(MeshName)
		if not Mesh then
			TRACE("ExtractTrisMode - invalid mesh!\n")
			ExtractTrisMode.CurFuncSeq = IdleFunc
			return
		end
		local TriIndex = {}
		for i=1,table.getn(SelectFaceMode.tIndex),3 do
			table.insert(TriIndex,math.floor(SelectFaceMode.tIndex[i]/3))
		end
		local OriScale = EditObj:GetScaling()
		local OriPos = EditObj:GetPos()
		local ExtPos = SelectFaceMode.FakeObj:GetPos()
		local ExtMesh = Mesh:Extract(TriIndex)
		if not ExtMesh then
			TRACE("ExtractTrisMode - Extraction failed\n")
			ExtractTrisMode.CurFuncSeq = IdleFunc
			return
		end
		local ExtTrans = {x=(OriPos.x-ExtPos.x)/OriScale.x,y=(OriPos.y-ExtPos.y)/OriScale.y,z=(OriPos.z-ExtPos.z)/OriScale.z}
		ExtMesh:Translation(ExtTrans)
		local NewMeshName = GenMeshName()
		g_MgrMesh:Add(NewMeshName,ExtMesh)

		local NewObj = cObj:New()
		NewObj:SetShader("mesh.fx")
		NewObj:SetMesh(NewMeshName)
		NewObj:SetPos(ExtPos)
		NewObj:SetScaling(OriScale)
		NewObj:SetAmbient(GenRandColor())
		local ObjName = GenObjName()
		g_MgrObj:Add(ObjName,NewObj)

		SelectFaceMode.tIndex = {}
		EditObj:ClearSelIndex()
		TRACE("ExtractTrisMode - Extraction succeeded!\n")
		ExtractTrisMode.CurFuncSeq = IdleFunc
	end,
	End = function ()
		ExtractTrisMode.CurFuncSeq = ExtractTrisMode.FuncSeq.Pass1
	end
}
}
